﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.GUIs.LibraryGUI;

namespace CapstoneProjectAEReader.GUIs.DictionaryGUI
{
    /// <summary>
    /// Interaction logic for NoteManagement.xaml
    /// </summary>
    public partial class NoteManagement : UserControl
    {
        #region Attributes

        private DAO _dao;
        private Note _selectedNote;
        private List<NoteDetail> _noteDetails;
        private NoteDetail _selectedNoteDetail;
        private MainWindow _parent;

        private const string NoteFormTitle = "noteFormTitle";
        private const string NoteList = "noteList";
        private const string NoteDetailList = "noteDetailList";
        private const string NoteDetail = "noteDetail";
        private const string BtDeleteNoteDetail = "deleteNoteDetail";
        private const string BtDeleteNote = "deleteNote";
        private const string BtApply = "applyButton";
        private const string BtEdit = "edit";
        private const string Ebook = "ebook";
        private const string Date = "datetime";
        private const string Description = "description";
        private const string DeleteNoteConf = "deleteNoteConfirmation";
        private const string DeleteNoteDetailConf = "deleteNoteDetailConfirmation";
        private const string OpenEbook = "statusOpenind";
        private const string DeleteNoteDetail = "deleteNoteDetail";
        private const string EditNoteDetail = "editNoteDetail";
        private const string GoToNoteDetail = "gotoNoteDetail";
        private const string DeleteNote = "deleteNote";
        private const string SearchNote = "searchNote";
        private const string SelectNoteDetailWarn = "selectNoteDetailWarn";
        private const string SelectNoteWarn = "selectNoteWarn";

        #endregion

        public NoteManagement(DTO dto, MainWindow parent)
        {
            _parent = parent;
            InitializeComponent();
            _dao = new DAO(dto);
            ReloadFormLanguage();
            RebindNoteList(_dao.Dto.Note);
        }

        public void ReloadFormLanguage()
        {
            lbNoteFormTitle.Content = ResourceReader.GetResourceContent(NoteFormTitle);
            lbNoteList.Content = ResourceReader.GetResourceContent(NoteList);
            lbNoteDetails.Content = ResourceReader.GetResourceContent(NoteDetailList);
            lbNoteDetail.Content = ResourceReader.GetResourceContent(NoteDetail);
            lbDelete.Content = ResourceReader.GetResourceContent(BtDeleteNoteDetail);
            lbApply.Content = ResourceReader.GetResourceContent(BtApply);
            lbDeleteNote.Content = ResourceReader.GetResourceContent(BtDeleteNote);
            //------------------------------------------------------------
            columnEbookTitle.Header = ResourceReader.GetResourceContent(Ebook);
            columnDate.Header = ResourceReader.GetResourceContent(Date);
            columnDescription.Header = ResourceReader.GetResourceContent(Description);
            var noteCtMenu = (ContextMenu) Resources["NoteDetailContextMenu"];
            MenuItem tempMenuItem = (MenuItem) noteCtMenu.Items[0];
            tempMenuItem.Header = ResourceReader.GetResourceContent(GoToNoteDetail);
            tempMenuItem = (MenuItem) noteCtMenu.Items[1];
            tempMenuItem.Header = ResourceReader.GetResourceContent(EditNoteDetail);
            tempMenuItem = (MenuItem) noteCtMenu.Items[2];
            tempMenuItem.Header = ResourceReader.GetResourceContent(DeleteNoteDetail);
            var noteTabCtMenu = (ContextMenu) Resources["NoteContextMenu"];
            tempMenuItem = (MenuItem) noteTabCtMenu.Items[0];
            tempMenuItem.Header = ResourceReader.GetResourceContent(DeleteNote);
            btSearch.ToolTip = ResourceReader.GetResourceContent(SearchNote);
        }

        public void RebindNoteList(List<Note> temp)
        {
            NoteListView.ItemsSource = null;
            NoteListView.ItemsSource = temp;
        }

        private void RebindNoteDetails(List<NoteDetail> noteDetails)
        {
            NoteDetailListView.ItemsSource = null;
            NoteDetailListView.ItemsSource = TransformNoteDetail(noteDetails);
        }

        private void RebindNoteDetail(NoteDetail temp)
        {
            Note tempNote = _dao.GetNoteById(temp.Noteid);
            txtNotedWord.Text = tempNote.NoteContent;
            txtTitle.Text = _dao.GetEbookById(temp.Bookid).Title;
            txtDate.Text = temp.Createdate;
            FlowDocument tempDoc = new FlowDocument();
            tempDoc.Blocks.Add(new Paragraph(new Run(temp.Description)));
            txtDescription.Document = tempDoc;
            txtPageNo.Text = temp.PageNumber.ToString();
        }

        private void SetBlankNoteDetail()
        {
            txtNotedWord.Text = Constants.CommonCharacters.Empty;
            txtTitle.Text = Constants.CommonCharacters.Empty;
            txtDate.Text = Constants.CommonCharacters.Empty;
            txtDescription.Document = new FlowDocument();
            txtPageNo.Text = Constants.CommonCharacters.Empty;
        }

        public Grid GetNoteManagementTab()
        {
            return NoteManagementGrid;
        }

        public List<DisplayNoteDetail> TransformNoteDetail(List<NoteDetail> noteDetails)
        {
            List<DisplayNoteDetail> rtList = new List<DisplayNoteDetail>();
            foreach (var item in noteDetails)
            {
                DisplayNoteDetail temp = new DisplayNoteDetail();
                temp.BookTitle = _dao.GetEbookById(item.Bookid).Title;
                temp.Date = item.Createdate;
                temp.Description = item.Description;
                temp.CurrentNoteDetail = item;
                rtList.Add(temp);
            }
            return rtList;
        }

        private void HandleSelectNote(object sender, SelectionChangedEventArgs e)
        {
            _selectedNote = (Note) NoteListView.SelectedItem;
            if (_selectedNote != null)
            {
                _noteDetails = _dao.GetNoteDetailWithId(_selectedNote.NoteId);
                RebindNoteDetails(_noteDetails);
                SetBlankNoteDetail();
            }
        }

        private void HandleUpdateButton(object sender, RoutedEventArgs e)
        {
            if (NoteDetailListView.SelectedItem != null)
            {
                DisplayNoteDetail selected = (DisplayNoteDetail) NoteDetailListView.SelectedItem;
                NoteDetail temp = selected.CurrentNoteDetail;
                RebindNoteDetail(temp);
                _selectedNoteDetail = temp;
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SelectNoteDetailWarn));
            }
        }

        private void HandleApplyButton(object sender, RoutedEventArgs e)
        {
            if (_selectedNoteDetail != null)
            {
                var textRange = new TextRange(txtDescription.Document.ContentStart, txtDescription.Document.ContentEnd);
                _selectedNoteDetail.Description = textRange.Text;
                _dao.UpdateNoteDetail(_selectedNoteDetail);
                RebindNoteDetails(_dao.GetNoteDetailWithId(_selectedNote.NoteId));
            }
        }

        private void HandleNoteDtLvMouseDbClick(object sender, RoutedEventArgs e)
        {
            if (NoteDetailListView.SelectedItems.Count == 1)
            {
                DisplayNoteDetail selectedItem = (DisplayNoteDetail) NoteDetailListView.SelectedItem;
                Ebook selectedEbook = _dao.GetEbookById(selectedItem.CurrentNoteDetail.Bookid);
                Message temp =
                    new Message(String.Format(ResourceReader.GetResourceContent(OpenEbook), selectedEbook.Title));
                temp.Show();
                _parent.OpenNewEbook(selectedEbook.Path, selectedItem.CurrentNoteDetail.PageNumber);
                temp.Close();
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SelectNoteDetailWarn));
            }
        }

        private void HandleSelectNoteDetail(object sender, RoutedEventArgs e)
        {
            if (NoteDetailListView.SelectedItems.Count > 0)
            {
                DisplayNoteDetail selected = (DisplayNoteDetail) NoteDetailListView.SelectedItem;
                NoteDetail temp = selected.CurrentNoteDetail;
                RebindNoteDetail(temp);
                _selectedNoteDetail = temp;
            }
        }

        private void HandleBtDeleteNoteDetail(object sender, RoutedEventArgs e)
        {
            if (NoteDetailListView.SelectedItems.Count == 1)
            {
                DisplayNoteDetail selectedItem = (DisplayNoteDetail) NoteDetailListView.SelectedItem;
                if (MessageBoxResult.OK ==
                    Utilities.ShowConfirmationMesBox(ResourceReader.GetResourceContent(DeleteNoteDetailConf)))
                {
                    _dao.DeleteNoteDetail(selectedItem.CurrentNoteDetail);
                    _noteDetails = _dao.GetNoteDetailWithId(_selectedNote.NoteId);
                    RebindNoteDetails(_noteDetails);
                    SetBlankNoteDetail();
                }
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SelectNoteDetailWarn));
            }
        }

        private void HandleBtDeleteNote(object sender, RoutedEventArgs e)
        {
            if (NoteListView.SelectedItems.Count == 1)
            {
                if (MessageBoxResult.OK ==
                    Utilities.ShowConfirmationMesBox(ResourceReader.GetResourceContent(DeleteNoteConf)))
                {
                    string searchedText = txtSearch.Text.Trim();
                    _dao.DeleteNote(_selectedNote);
                    RebindNoteList(_dao.SearchNote(searchedText));
                    RebindNoteDetails(new List<NoteDetail>());
                    SetBlankNoteDetail();
                }
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(SelectNoteWarn));
            }
        }

        private void HandleBtSearch(object sender, RoutedEventArgs e)
        {
            string searchedText = txtSearch.Text.Trim();
            RebindNoteList(_dao.SearchNote(searchedText));
        }
    }

    public class DisplayNoteDetail
    {
        private string _bookTitle;
        private string _date;
        private string _description;

        public string BookTitle
        {
            get { return _bookTitle; }
            set { _bookTitle = value; }
        }

        public string Date
        {
            get { return _date; }
            set { _date = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public NoteDetail CurrentNoteDetail
        {
            get { return _currentNoteDetail; }
            set { _currentNoteDetail = value; }
        }

        private NoteDetail _currentNoteDetail;
    }
}